package com.android.nearshop.app.adapter.listadapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.android.nearshop.app.adapter.listadapter.Text3ListAdapter.Text3Item;
import com.android.nearshop.app.widget.Text3ItemView;
import com.android.nearshop.app.widget.Text3ItemView.OnMCheckedChangeListener;
import com.android.nearshop.io.protocol.out.Catalogy;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-16
 * @see :    商家分类
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class CategoryListAdapter extends ArrayExpandListAdapter<Catalogy,Catalogy>
    implements OnMCheckedChangeListener
{
 
    private boolean isMutileSel = false;
    public CategoryListAdapter(Context context)
    {
        this(context,false);
    }
    
    public CategoryListAdapter(Context context,boolean isMutileSel)
    {
        super(context);
        this.isMutileSel = isMutileSel;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent)
    {
        Text3ItemView row = null;
        Catalogy item = (Catalogy)getChild(groupPosition, childPosition);
        item.setMutilSel(isMutileSel);
        if(convertView == null)
        {
            row = new Text3ItemView(mContext);
            row.setBackgroundColor(0xFFE6E6E6);
        }else
        {
            if(convertView instanceof Text3ItemView)
            {
                row = (Text3ItemView)convertView;
            }
        }
        row.bindData(item);
        row.setOnMCheckedChangeListener(this);
        return row;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent)
    {
        Text3ItemView row = null;
        Catalogy item = (Catalogy)getGroup(groupPosition);
        item.setMutilSel(isMutileSel);
        if(convertView == null)
        {
            row = new Text3ItemView(mContext);
            row.setBackgroundColor(0xFFF2F2F2);
        }else
        {
            if(convertView instanceof Text3ItemView)
            {
                row = (Text3ItemView)convertView;
            }
        }
        row.bindData(item, isExpanded);
        row.setOnMCheckedChangeListener(this);
        return row;
    }

    public ExpandData<Catalogy, Catalogy> getAllHeader()
    {
        Catalogy all = new Catalogy();
        all.catalogyId = -1;
        all.catalogyName="全部分类";
        return new ExpandData<Catalogy, Catalogy>(all,null);
    }
    
    public ArrayList<Integer> getSelIds()
    {
        ArrayList<Integer> ids = new ArrayList<Integer>();
        int size = getGroupCount();
        Catalogy group = null;
        Catalogy child = null;
        group = (Catalogy)getGroup(0);
        if(group == null || (group.checked(false, true, false) && group.catalogyId < 0))
        {
            // 选择了全部,返回null
            return ids;
        }
        // 选择了一部分
        for(int i = 1 ; i < size; i++)
        {
             group = (Catalogy)getGroup(i);
             if(group.checked(false, true, false))
             {
                 ids.add(group.catalogyId);
             }else
             {
                 int chSize = getChildrenCount(i);
                 for(int j= 0; j < chSize; j++)
                 {
                     child = (Catalogy)getChild(i, j);
                     if(child.checked(false, true, false))
                     {
                         ids.add(child.catalogyId);
                     }
                 }
             }
        }
        return ids;
    }

    @Override
    public void onCheckedChanged(Text3Item data, boolean isChecked)
    {
        Catalogy item = (Catalogy)data;
        if(item.catalogyId < 0)
        {
            // 全部
            int size = getGroupCount();
            for(int i = 0; i < size; i++)
            {
                Catalogy group = (Catalogy)getGroup(i);
                group.checked(false, false, isChecked);
                group.setCheckForChild();
            }
            notifyDataSetChanged();
        }
        if(!item.isChild())
        {
            // 一级
            if(item.setCheckForChild())
            {
                notifyDataSetChanged();
            }
        }else
        {
            // 二级
            if( item.parent != null)
            {
                if(!isChecked)
                {
                    item.parent.checked(false, false, false);
                }else
                {
                    if(item.parent.isAllSel())
                    {
                        item.parent.checked(false, false, true);
                    }
                }
                notifyDataSetChanged();
            }
        }
    }
}
